<?php


    // Author: Tomasz Durka
    // Date: 5th February 2007
    // Prints out a link for PHP nails
    
    
  function sortable($params) {

    $module = mod_suffix ? '/' . mod_suffix : '';
    $controller = module;
    $action = action;
    $parameter = false;
    $prefix = '';
    $handle = '';
    $constraint = '';
    $overlap = '';
    $drop_on_empty = '';
    $update = '';
    $url = '';
    $only = '';
    $tag = '';
    $startup_request = false;
    $script_embed = true;
    
    
    if (isset($params['active_if'])) {
      $active_if = "\$active_if = ({$params['active_if']});";
      eval($active_if);
      if ($active_if)
        $class = ' class="active"';
    }
    
    foreach($params as $_key => $_val) {
      switch ($_key) {
        case 'lists':
            $$_key = is_array($_val) ? $_val : preg_split('/[ \t]*,[ \t]*/', $_val);
          break;
        case 'update':
          $update = "'{$_val}', ";
          break;
        case 'prefix':
        case 'startup_request':
        case 'script_embed':
          $$_key = $_val;
          break;
        case 'constraint':
        case 'overlap':
        case 'handle':
        case 'only':
        case 'tag':
            if ($_val === false) {
              $$_key = ", {$_key}: false";
            } elseif ($_val === true) {
              $$_key = ", {$_key}: true";
            } else {
              $$_key = ", {$_key}: '{$_val}'";
            }
          break;
        case 'drop_on_empty':
          $drop_on_empty = ", dropOnEmpty: '{$_val}'";
          break;
        case 'on_update':
          $on_update = ", onUpdate: {$_val}";
          break;
        case 'url':
          if ($_val ==  'back')
            $$_key = $_SERVER['HTTP_REFERER'];
          else
            $$_key = '/' . $_val;
          break;
        default:
          logger()->warning('Incorrect attribute for <i>sortable</i> function');
          break;
      }
    }
    
    if (!isset($lists)) {
      logger()->warning('No <b>lists</b> attribute specified');
    }
    $ret = '';
    
    if ($script_embed)    
      $ret .= "<script type=\"text/javascript\">// <![CDATA[\n";
    
    foreach ($lists as $list) {
      
      if ($update != '') {
        $ajax_begin = 'Updater';
      } else {
        $ajax_begin = 'Request';
      }
      $ajax_call = "new Ajax.{$ajax_begin}({$update}'{$url}', {onComplete:function(request){}, parameters:" . join("+'&'+", array_map(create_function('$arg', 'return "Sortable.serialize(\'' . $prefix . '{$arg}\')";'), $lists)) . ", evalScripts:true, asynchronous:true});";
      $ret .= "Sortable.create('{$prefix}{$list}', {dropOnEmpty: true, containment:['{$prefix}" . join("', '{$prefix}", $lists) . "'], onUpdate:function(){{$ajax_call}}{$handle}{$only}{$constraint}{$overlap}{$tag}});";
    }
    if ($startup_request)
      $ret .= "{$ajax_call}\n";
      
    if ($script_embed)
      $ret .= "// ]]></script>";
      
    //pc($ret, true);
    return $ret;
  }

?>